package me.dz.dreamcleaner.runnable;

import me.dz.dreamcleaner.manager.WorldManager;
import me.dz.dreamcleaner.utils.ClearUtils;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Entity;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;

import static me.dz.dreamcleaner.DreamCleaner.*;
import static me.dz.dreamcleaner.DreamCleaner.mobsBarMsg;
import static me.dz.dreamcleaner.manager.FlexibleFormula.*;

public class ClearTask extends TimerTask {

    private int item = 0;
    private int mobs = 0;
    public static BossBar itemBossBar;
    public static BossBar mobsBossBar;

    @Override
    public void run() {
        if (itemEnable){
            item++;
            int time = itemTime - item;
            if (itemMsg && itemRemind.contains(time)){
                new BukkitRunnable(){
                    @Override
                    public void run() {
                        Bukkit.broadcastMessage(itemClearMsg.get("remind")
                                .replace("<n>",time+"")
                                );
                    }
                }.runTask(pl);
            }
            if (itemBar){
                if (time <= itemBarTime){
                    itemBossBar.setTitle(itemBarMsg
                            .replace("<n>",time+""));
                    double d = Math.max(0,(double) time / itemBarTime);
                    itemBossBar.setProgress(d);
                    Bukkit.getOnlinePlayers().forEach(p -> itemBossBar.addPlayer(p));
                }
            }
            if (itemAction){
                if (time <= itemBarTime){
                    String msg = itemBarMsg
                            .replace("<n>",time+"");
                    Bukkit.getOnlinePlayers().forEach(p -> p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(msg)));
                }
            }
            if (item >= itemTime){
                item = 0;
                new BukkitRunnable(){
                    @Override
                    public void run() {
                        HashMap<World,ArrayList<Entity>> map = WorldManager.getEntities(itemBlackWorld,itemCheck);
                        if (itemAsync){
                            new BukkitRunnable(){
                                @Override
                                public void run() {
                                    ClearUtils.runItem(map);
                                }
                            }.runTaskAsynchronously(pl);
                        }else {
                            ClearUtils.runItem(map);
                        }
                    }
                }.runTask(pl);
                if (itemBar){
                    itemBossBar.removeAll();
                }
            }
        }

        if (mobsEnable){
            mobs++;
            int time = mobsTime - mobs;
            if (mobsMsg && mobsRemind.contains(time)){
                new BukkitRunnable(){
                    @Override
                    public void run() {
                        Bukkit.broadcastMessage(mobsClearMsg.get("remind")
                                .replace("<n>",time+"")
                                );
                    }
                }.runTask(pl);
            }
            if (mobsBar){
                if (time <= mobsBarTime){
                    mobsBossBar.setTitle(mobsBarMsg
                            .replace("<n>",time+""));
                    double d = Math.max(0,(double) time / mobsBarTime);
                    mobsBossBar.setProgress(d);
                    Bukkit.getOnlinePlayers().forEach(p -> mobsBossBar.addPlayer(p));
                }
            }
            if (mobsAction){
                if (time <= mobsBarTime){
                    String msg = mobsBarMsg
                            .replace("<n>",time+"");
                    Bukkit.getOnlinePlayers().forEach(p -> p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(msg)));
                }
            }
            if (mobs >= mobsTime){
                mobs = 0;
                new BukkitRunnable(){
                    @Override
                    public void run() {
                        HashMap<World,ArrayList<Entity>> map = WorldManager.getEntities(mobsBlackWorld,mobsCheck);
                        AtomicInteger i = new AtomicInteger();
                        map.forEach((a,b) -> i.addAndGet(b.size()));
                        int amount = (int) mathematical(mobsAmount
                        .replace("<online>",WorldManager.getOnline())
                        .replace("<players>",WorldManager.getPlayers()));
                        if (!mobsMeet || (mobsServer && i.get() >= amount) || (mobsMeet && !mobsServer)){
                            if (mobsAsync){
                                new BukkitRunnable(){
                                    @Override
                                    public void run() {
                                        ClearUtils.runMobs(map,false);
                                    }
                                }.runTaskAsynchronously(pl);
                            }else {
                                ClearUtils.runMobs(map,false);
                            }
                        }else {
                            if (mobsMsg){
                                new BukkitRunnable(){
                                    @Override
                                    public void run() {
                                        Bukkit.broadcastMessage(mobsClearMsg.get("amount"));
                                    }
                                }.runTask(pl);
                            }
                        }
                    }
                }.runTask(pl);
                if (mobsBar){
                    mobsBossBar.removeAll();
                }
            }
        }
    }
}
